class Solution {
public:
    int rotatedDigits(int n) {
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            string s = to_string(i);
            int flag = 0;
            for (auto& x : s)
            {
                if (x == '1' || x == '0' || x == '8') continue;
                else if (x == '5' || x == '2' || x == '6' || x == '9') flag = 1;
                else
                {
                    flag = 0; break;
                }
            }
            ans += flag;
        }
        return ans;
    }
};